import 'dart:convert';

import 'package:du_application/http/HttpUtil.dart';
import 'package:du_application/http/Options.dart';
import 'package:du_application/model/CollectModel.dart';
import 'package:du_application/model/IndexModel.dart';
import 'package:du_application/model/ResultModel.dart';

import 'package:flutter/material.dart';
import 'package:provide/provide.dart';
import 'package:du_application/provide/AuthProvider.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AffairProvider with ChangeNotifier {
    CollectModel collectModel;
    Tips tips;
    bool isGoods=false;
    String content;
    String url;
    Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
   getCollect(context) async {
    return await _prefs.then((ins) async{
    Options options =  Options(Provide.value<AuthProvider>(context).token);
    return await HttpUtil.getInstance().get("/collect/list",data: {"type":0},options:options.options).then((data) {
      collectModel=CollectModel.fromJson(json.decode(data));
       notifyListeners();
     return true;
     });
    });
  }
  setTopic(Listm goods,double offsetx,double offsety){
    this.tips=Tips(goods:goods,offsetx: offsetx,offsety:offsety);
    this.isGoods=true;

   notifyListeners();
  }
  addTopic(context) async {

 return await _prefs.then((ins) async{
    Options options =  Options(Provide.value<AuthProvider>(context).token);
    return await HttpUtil.getInstance().post("/affair/add",data: {
      "isGoods":isGoods,
       "goodsId":isGoods?tips.goods.valueId:null,
       "content":content,
       "url":url
    },options:options.options).then((data) {
       ResultModel resultModel=ResultModel.fromJson(json.decode(data));
   
       notifyListeners();
       return true;
     });
    });

  }

  void record() {
    tips=null;
    isGoods=false;
 content=null;
    url=null;
     notifyListeners();
  }

  void setImage(String url) {

  this.url=url;
   notifyListeners();
  }

  void setContent(String value) {
    this.content=value;
       notifyListeners();
  }
}
  

class Tips{
    double offsetx;
    double offsety;
    Listm goods;
   
    Tips({this.goods,this.offsetx,this.offsety});
}
